var task = {
    config: {},
    /*
     * 任务类型绑定
     */
    Typebind: function () {
        var _this = this,
                $dtype = $(".nav-task-type a");

        $dtype.click(function () {
            var $curid = $(this).attr("data-id"),
                    $areadiv = _this.config.areaType[$curid + '-' + _this.config.gradeid];
            $("input[name='type']").val($curid);
            //判断区域
            if (typeof $areadiv.isarea !== "undefined" && $areadiv.isarea) {
                $("#selectquyu").show();
            } else {
                $("#selectquyu").hide();
            }
            //判断选手
            if (typeof $areadiv.xuanshou !== "undefined" && $areadiv.xuanshou) {
                $("#xuanshou").show();
            } else {
                $("#xuanshou").hide();
            }
            //选手标签、提示
            if (typeof $areadiv.xuanshou_label !== "undefined" && $areadiv.xuanshou_label) {
                $("#xuanshou .control-label").text($areadiv.xuanshou_label);
                $("#xuanshou input").attr("placeholder", $areadiv.xuanshou_placeholder);
            }
            //任务链接标签、提示
            if (typeof $areadiv.tasklink_label !== "undefined" && $areadiv.tasklink_label) {
                $("#tasklink .control-label").text($areadiv.tasklink_label);
                $("#tasklink input").attr("placeholder", $areadiv.tasklink_placeholder);
            }
            //判断是否提供手机账号
            if (typeof $areadiv.is_offer_mobile !== "undefined" && $areadiv.is_offer_mobile) {
                $("#tszh").show();
            } else {
                $("#tszh").hide();
            }
            //设定单价
            var t = [];
            $.each($areadiv.priceData, function (k, v) {
                if (_this.config.priceSelected == v.id) {
                    t.push('<li class="active"><a href="#' + v.id + '" data-toggle="tab" data-id="' + v.id + '">' + v.value + '</a></li>');
                } else {
                    t.push('<li><a href="#' + v.id + '" data-toggle="tab" data-id="' + v.id + '">' + v.value + '</a></li>')
                }
            });
            $("#danjia").empty().html(t.join(""));
            $("#danjia li a").unbind("click");
            $("#danjia li a").bind("click", function () {
                $("input[name='task_price_id']").val($(this).attr("data-id"));
                $(this).parent().addClass("active");
                $(this).parent().siblings().removeClass("active");
            });
            if ($("#danjia li.active").length === 0) {
                $("#danjia li a:first").click();
            } else {
                $("#danjia li.active a").click();
            }
            //设定属性placeholder
            if (typeof $areadiv.title !== "undefined" && $areadiv.title) {
                $("#name").attr("placeholder", $areadiv.title);
            } else {
                $("#name").attr("placeholder", "\u5982\u9700\u62fc\u56fe\u8bf7\u4ed4\u7ec6\u8bf4\u660e\u8981\u62fc\u524d\u540e\u56fe\u7247");
            }
            if (typeof $areadiv.note !== "undefined" && $areadiv.note) {
                $("#jianjie").attr("placeholder", $areadiv.note);
            } else {
                $("#jianjie").attr("placeholder", "\u516c\u4f17\u53f7\u6216\u94fe\u63a5\u5730\u5740\u53ef\u4ee5\u5199\u5230\u4e0a\u9762\uff0c\u65b9\u4fbf\u4f1a\u5458\u590d\u5236\uff0c\u63d0\u9ad8\u4efb\u52a1\u6548\u7387\uff0c\u8bf7\u5728\u8fd9\u91cc\u5199\u6e05\u695a\u4efb\u52a1\u6d41\u7a0b\u548c\u622a\u56fe\u6807\u51c6\uff01\u6295\u7968\u5185\u5bb9\u63cf\u8ff0\u8d8a\u6e05\u6670\uff0c\u505a\u7684\u901f\u5ea6\u8d8a\u5feb\uff01");
            }
        });
        if (typeof _this.config.areaSelected !== "undefined" && _this.config.areaSelected > 0) {
            $(".nav-task-type a[data-id='" + _this.config.areaSelected + "']").click();
        } else {
            $dtype.first().click();
        }
    },
    /*
     * 限定速度配置
     */
    Speedbind: function () {
        var _this = this,
                $speed = $("input[name='is_speed']");
        $speed.click(function () {
            var $cur = $(this).val();
            if (parseInt($cur) === 1) {
                $("#sudu").show();
            } else {
                $("#sudu").hide();
                $("#xzNO").val(0);
            }
        });
        if (_this.config.speedtype == "" || _this.config.speedtype == null) {
            _this.config.speedtype = 0;
        }
        $("#xiansu_" + _this.config.speedtype).click();
        if (_this.config.mobiletype == "" || _this.config.mobiletype == null) {
            _this.config.mobiletype = 0;
        }
        $("#toushouzh_" + _this.config.mobiletype).click();
    },
    /*
     * 上传图片绑定
     */
    upImgbind: function (conf) {
        $("#qrpickfiles").fileinput({
            showCaption: false,
            browseClass: "btn orange",
            browseLabel: "上传样图",
            browseIcon: "<i class=\"glyphicon glyphicon-picture\"></i> ",
            showRemove: false,
            showUpload: false
        });
        $("#qrpickfilesqrcode").fileinput({
            showCaption: false,
            browseClass: "btn orange",
            browseLabel: "点击上传二维码",
            browseIcon: "<i class=\"glyphicon glyphicon-picture\"></i> ",
            showRemove: false,
            showUpload: false
        });
        $(".stepimage").fileinput({
            showCaption: false,
            browseClass: "btn orange",
            browseLabel: "添加步骤图片",
            browseIcon: "<i class=\"glyphicon glyphicon-picture\"></i> ",
            showRemove: false,
            showUpload: false
        });
    },
    init: function (conf) {
        $.extend(this.config, conf);
        this.Typebind();
        this.Speedbind();
        this.areaBtn();
        this.addStep();
    },
    addStep:function(){
        $(".addstep").click(function(){
           var $index = $(".stepclone").length;
           var $newclass = "stepimage"+$index;
           var $cl = $(".clonediv").clone(false);
           var $pt = $(this).parent().parent();
           $cl.removeClass("clonediv").addClass("stepclone").show();
           $("input",$cl).removeClass("stepimage").addClass($newclass);
           $("input",$cl).attr("name","stepfile"+$index+"[]");
           $("textarea",$cl).attr("name","step"+$index);
           $pt.before($cl);
           $("input[name='stepcount']",$pt).val($index);
           $("."+$newclass).fileinput({
                showCaption: false,
                browseClass: "btn orange",
                browseLabel: "添加步骤图片",
                browseIcon: "<i class=\"glyphicon glyphicon-picture\"></i> ",
                showRemove: false,
                showUpload: false
            });
        });
    },
    /*
     * 地区选择
     * @returns {undefined}
     */
    areaBtn: function () {
        $("#province").change(function () {
            var that = $(this);
            $.getJSON(that.attr("ajaxurl"), {area: that.val()}, function (data) {
                if (data.status === true) {
                    $("#perNO").empty().html(data.num + "人");
                }
            });
        });
    },
    /*
     * 功能菜单
     * 
     */
    bindFunc: function () {
        $(".dropdown-menu-function a").click(function () {
            var that = $(this);
            if (that.attr("data-action") === "update") {
                $.getJSON("/task/ajaxcheckupdate.html", {}, function (data) {
                    if (data.status === true) {
                        window.location.href = that.attr("data-url");
                    } else {
                        var d = dialog({
                            title: '提示',
                            content: data.msg,
                            ok: function () {
                                window.location.href = data.url;
                            },
                            okValue: "确认",
                            cancelValue: '取消',
                            cancel: function () { }
                        });
                        d.showModal();
                    }
                });

            } else {
                window.location.href = that.attr("data-url");
            }
        });
        $("a.operateTask").bind("click", function () {
            var that = $(this),
                    $id = that.attr("data-id"),
                    $action = that.attr("data-action"),
                    $confirmTip = "";
            if ($action === "stoptask") {
                $confirmTip = "确认要停止投票吗？未完成的部分将退还余额";
            } else if ($action === "dabao") {
                $confirmTip = "确认要打包吗？";
            }
            if (typeof $action !== "undefined" && $action !== "" && $action !== null) {
                if (confirm($confirmTip)) {
                    $.post("/task/ajaxoperate.html", {id: $id, action: $action}, function (data) {
                        var d = dialog({
                            title: "提醒",
                            content: data.msg,
                            ok: function () {
                                window.location.reload();
                            },
                            okValue: "确认"
                        }).show();
                    }, "json");
                }
            }
        });
    },
    /*
     * 展示图片
     */
    showPic: function () {
        $(".showimgs").click(function () {
            var ds = dialog({
                title: "查图",
                content: '<img width=\"480\" src="' + $(this).attr("bigsrc") + '"/>',
            }).show();
        });
        $(".btn-danger").bind("click", function () {
            var id = $(this).attr("value");
            var that = $(this);
            var d = dialog({
                title: '投诉',
                content: '<div style="text-align:right"> <br/>截屏中显示已经投过的，或者其他不是正常提示投票成功<input type="radio" name="yuanyin" value="截屏中显示已经投过的，或者其他不是正常提示投票成功的，将无佣金" checked="checked" /> <br/>  上传图片对应投票任务错误<input type="radio" name="yuanyin" value="上传图片对应投票任务错误"  /><br/>没有按照投票要求连续投票<input type="radio" name="yuanyin" value="没有按照投票要求连续投票" /> <br/>禁止上传样图<input type="radio" name="yuanyin" value="禁止上传样图" /><br/> 没有按照投票提示操作，需要投票后出现成功投票的提示<input type="radio" name="yuanyin" value="没有按照投票提示操作，需要投票后出现成功投票的提示" /><br/>没有按照投票提示去投票，不可以在这个地方截屏上传<input type="radio" name="yuanyin" value="没有按照投票提示去投票，不可以在这个地方截屏上传" /><br/>自定义：<input id="zdy" style="width:400px" name="zdy"></div>',
                ok: function () {
                    var content = $('input:radio[name="yuanyin"]:checked').val();
                    var zdy = $('#zdy').val();
                    $.post("/task/ajaxcomplain.html", {"id": id, "con": content, "zdy": zdy}, function (data) {
                        if (data.status === true) {
                            that.removeClass("btn-danger").addClass("btn-primary").html("已投诉");
                            that.unbind("click");
                            alert(data.msg);
                        } else {
                            alert(data.msg);
                        }
                    }, "json");
                },
                okValue: "确认",
                cancelValue: '取消',
                cancel: function () { }
            });
            d.showModal();
        });
    },
    checkimage: function () {
        $(".imgshow").click(function () {
            var ds = dialog({
                title: "查图",
                content: '<img width=\"480\" src="' + $(this).attr("imgsrc") + '"/>'
            }).show();
        });
    }

};